#!/bin/bash

# This file is part of libmarco
# http://code.google.com/p/libmarco/

# Tracklaptop.sh
# Tracks your laptop using Twitter

# This version does not play well with Linux. 
# Tested only on OS X Leopard and Snow Leopard.

# Add to ROOT crontab as:
# 00 * * * * /var/root/tracklaptop.sh 2>&1  >> /var/log/tracklaptop.log
# 15 * * * * /var/root/tracklaptop.sh 2>&1  >> /var/log/tracklaptop.log
# 30 * * * * /var/root/tracklaptop.sh 2>&1  >> /var/log/tracklaptop.log
# 45 * * * * /var/root/tracklaptop.sh 2>&1  >> /var/log/tracklaptop.log

# Twitter parameters, use a PRIVATE account!
# (A single account works fine for multiple computers)
user="your-twitter-username"
pass="your-twitter-password"

# Location of location info files created locally (Don't use ~ in the path!)
last_location="/var/root/.locationtrack.posted.txt"
current_location="/var/root/.locationtrack.now.txt"

## Uncomment to print the various steps
# verbose="true"
## Uncomment to force the script to post location every time it is executed
## (Normally it posts only if the location changes from last post)
# log_no_matter_what="true"

function log_message () {
    if [[ "$verbose" == "true" ]]; then
        echo "$1";
    fi
}

function compose_current_location () {
    local hostname=`hostname`
    log_message "Hostname: $hostname"
    
    local public_ip=`curl -s http://checkip.dyndns.org | sed -e 's/.*Current IP Address: //1' -e 's/<\/body>.*//1'`
    if [[ "$?" != "0" ]]; then
        log_message "Cannot determine current IP, no point in trying to update..."
        exit;
    fi
    log_message "Public IP: $public_ip"
    
    local local_ips=`/sbin/ifconfig |grep "inet " | grep -v "127.0.0.1" | awk '{print $2}' | xargs`;
    log_message "Local IPs: $local_ips"
    
    local macs=` /sbin/ifconfig |grep "ether " | awk '{print $2}' | xargs`
    log_message "MAC addresses: $macs"
    
    touch $current_location
    echo "H:$hostname"  >  "$current_location"
    echo "P:$public_ip" >> "$current_location"
    echo "L:$local_ips" >> "$current_location"
    echo "M:$macs"      >> "$current_location"
    
    if [[ "$verbose" == "true" ]]; then
        echo
        local ccount=`cat $current_location | wc -c | awk '{print $1}'`
        echo " *** Current log ($ccount characters) *** "
        cat $current_location | fold -s  -w 40
        echo
        echo "--"
        echo
    fi
}

function location_changed () {
	if [[ "$log_no_matter_what" == "true" ]]; then
		log_message "Current setting logs location even if it did not change"
		return 0;
	elif [[ ! -e "$last_location" ]]; then
		log_message "Last location not found!"
		return 0;
	elif [[ `diff $current_location $last_location` ]]; then
		log_message "Location changed!"
		return 0;
	else
		log_message "Location is the same as the last time!"
		return 1;
	fi
}

function post_location () {
    log_message "Posting log"

    statusmsg=`cat $current_location | tr ' ' '+' | tr '\n' '+'`
    
    date
    echo "Posting $statusmsg"

    curl -s --basic --user "$user:$pass" --data-ascii "status=$statusmsg" "http://twitter.com/statuses/update.json" > /dev/null
    if [[ $? != 0 ]]; then
        echo "Post failed"
        exit 1
    fi
    log_message "Post successful, saving as last location"
    mv $current_location $last_location
    echo "Post successful"
}


compose_current_location
location_changed
if [[ "$?" == "0" ]]; then
    post_location
fi
